<template>
  <van-row class="page-content-item-list">
    <van-nav-bar
      left-text="返回"
      title="选择考核内容"
      @click-left="$emit('onCancel')"/>
    <van-row class="page-content-item-list-header">
      <span>{{carrier.id + ' - ' +carrier.item}}</span>
    </van-row>
    <van-divider dashed>考核内容</van-divider>
    <card-content-item
      v-for="(content) in contentList"
      :key="content.id"
      :content="content"
      @click="doClick"/>
    <loading-overlay :show="isLoading"/>
    <van-popup
      v-model="showPageScoreItemList"
      position="right"
      get-container="body"
      style="width: 100%; height: 100%; background-color: #F9F9F9">
      <page-sore-item-list v-if="showPageScoreItemList" :carrier="carrier" :type="$vo.CarrierType.SUB" v-on:onConfirm="endSelectScoreItem"  v-on:onCancel="showPageScoreItemList = false"/>
    </van-popup>
  </van-row>
</template>

<script>
import PageSoreItemList from '@/views/common/page/PageSoreItemList'
import CardContentItem from '@/views/common/card/CardContentItem'
import { getCarrierDetail } from '@/api/assess'
export default {
  name: 'PageContentItemList',
  components: { PageSoreItemList, CardContentItem },
  props: ['carrier', 'type'],
  data () {
    return {
      isLoading: false,
      showPageScoreItemList: false,
      contentList: [],
      selectedContent: {
        item: '',
        id: ''
      }
    }
  },
  mounted () {
    this.doGetCarrierDetail()
  },
  methods: {
    doClick (content) {
      this.selectedContent = content
      this.selectedContent.sub_items.sort((obj1, obj2) => {
        var val1 = Number(obj1.id.split('.')[2])
        var val2 = Number(obj2.id.split('.')[2])
        if (val1 < val2) {
          return -1
        } else {
          return 1
        }
      })
      this.carrier.content_item.id = this.selectedContent.id
      this.carrier.content_item.item = this.selectedContent.item
      this.carrier.content_item.sub_items = this.selectedContent.sub_items
      this.showPageScoreItemList = true
    },
    endSelectScoreItem (scoreItems) {
      let content = {}
      content.id = this.selectedContent.id
      content.item = this.selectedContent.item
      content.score_item = scoreItems
      this.$emit('onConfirm', content)
      this.showPageScoreItemList = false
    },
    doGetCarrierDetail () {
      this.isLoading = true
      getCarrierDetail(this.carrier.id, this.type).then(res => {
        this.isLoading = false
        let { data } = res
        if (data[this.$response.CODE] === this.$response.SUCCESS) {
          this.contentList = data[this.$response.DATA].check_detail
          this.contentList.sort((obj1, obj2) => {
            var val1 = Number(obj1.id.split('.')[1])
            var val2 = Number(obj2.id.split('.')[1])
            if (val1 < val2) {
              return -1
            } else {
              return 1
            }
          })
        } else {
          this.$toast.fail('获取数据失败：' + data[this.$response.MESSAGE])
        }
      }, errFunc => {
        this.isLoading = false
        this.$notify('服务调用失败：' + errFunc.toString())
        console.log(errFunc.toString())
      })
    }
  }
}
</script>

<style scoped lang="less">
  .page-content-item-list{
    .page-content-item-list-header {
      background-color: white;
      line-height: 40px;
      text-align: center;
      font-size: 14px;
      color: #1989FA;
    }
  }
</style>
